﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
using System.Globalization;
using System.Resources;
using DataAccess.DataObject;
using EnmineKaitlin.Common;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class edit_coupon : Common.PageBase
    {
        int CouponID = 0;
        const string module = "manage-post";        
        int currentpage = 1;
        const int pagesize = 10;
        DateTime startdate = DateTime.Now - TimeSpan.FromDays(7);
        DateTime enddate = DateTime.Now + TimeSpan.FromDays(7);
        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.Write(Request.Params["cat_type"]);
            if (CheckPermission(module))
            {
                if (!IsPostBack)
                {
                    GetParam();
                    if (Request.Params["couponid"] != null)
                    {
                        CouponID = int.Parse(Request.Params["couponid"].ToString());
                    }
                    if (CouponID > 0)
                    {
                        BindDetailCoupon(CouponID);
                    }
                    BindCoupon();                    
                }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        
        protected void BindDetailCoupon(int CouponID)
        {
            IList<OCoupon> lstCoupon = _BCoupon.Get(CouponID);            
            txtName.Text = lstCoupon[0].Name;            
            txtContent.Text = lstCoupon[0].Content;
            txtStartDate.Text = lstCoupon[0].StartDate.ToString();
            txtEndDate.Text = lstCoupon[0].EndDate.ToString();
            txtDiscountPercent.Text = lstCoupon[0].DiscountPercent.ToString();
            ddlStatus.SelectedValue = lstCoupon[0].Status;
            hdfFeaturedImage.Value = lstCoupon[0].FeaturedImage;
            imgFeaturedImage.ImageUrl = lstCoupon[0].FeaturedImage;
            btnAddCat.Text = "Cập nhật";
            btnAddCat.CommandName = "Update";
        }
        /// <summary>
        /// Thêm mới chuyên mục
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddCat_Click(Object sender, CommandEventArgs e)
        {
            string cat_type = "Coupon";
            if (Request.Params["cat_type"] != null)
            {
                cat_type = Request.Params["cat_type"].ToString();
            }
            switch (e.CommandName)
            {
                case "Add":
                    _OCoupon.Name = txtName.Text;
                    _OCoupon.Content = txtContent.Text;
                    _OCoupon.StartDate = DateTime.Parse(txtStartDate.Text);
                    _OCoupon.EndDate = DateTime.Parse(txtEndDate.Text);
                    _OCoupon.DiscountPercent = float.Parse(txtDiscountPercent.Text);
                    _OCoupon.Status = ddlStatus.SelectedValue;
                    _OCoupon.FeaturedImage = hdfFeaturedImage.Value;                                        
                    if (_BCoupon.Add(_OCoupon))
                    {
                        txtName.Text = "";
                        txtContent.Text = "";
                        txtStartDate.Text = "";
                        txtEndDate.Text = "";
                        hdfFeaturedImage.Value = "";
                        imgFeaturedImage.ImageUrl = "";
                        BindCoupon();
                    }
                    break;
                case "Update":
                    if (Request.Params["couponid"] != null)
                    {
                        CouponID = int.Parse(Request.Params["couponid"].ToString());
                    }
                    if (CouponID > 0)
                    {
                        _OCoupon.CouponID = CouponID;
                        _OCoupon.Name = txtName.Text;
                        _OCoupon.Content = txtContent.Text;
                        _OCoupon.StartDate = DateTime.Parse(txtStartDate.Text);
                        _OCoupon.EndDate = DateTime.Parse(txtEndDate.Text);
                        _OCoupon.DiscountPercent = float.Parse(txtDiscountPercent.Text);
                        _OCoupon.Status = ddlStatus.SelectedValue;
                        _OCoupon.FeaturedImage = hdfFeaturedImage.Value;                        
                        if (_BCoupon.Update(_OCoupon))
                        {
                            BindCoupon();
                            BindDetailCoupon(CouponID);
                        }
                    }
                    break;
            }
        }
        protected void GetParam()
        {                        
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
        }
        protected void BindCoupon()
        {            
            IList<OCoupon> lstCoupon = _BCoupon.Get("",ddlStatusFilter.SelectedValue,"DESC","StartDate",currentpage,pagesize);
            grvCoupon.DataSource = lstCoupon;
            grvCoupon.DataBind();
            if (lstCoupon.Count > 0)
            {
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstCoupon[0].TotalResult;
                string url = "";                
                ctlPagging.QueryStringParameterName = url;
                ctlPagging.CurrentIndex = currentpage;
            }
        }
        protected void ddlStatusFilter_IndexChanged(object sender, EventArgs e)
        {
            BindCoupon();
        }
        /// <summary>
        /// Xóa chuyên mục
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvCoupon_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int CouponID = int.Parse(e.CommandArgument.ToString());                                                    
                if (true)
                {
                    RegisterClientScriptBlock("NOTE", "<script>alert('Đã xóa khuyến mại !');</script>");
                    BindCoupon();
                }
                else
                {
                    RegisterClientScriptBlock("NOTE", "<script>alert('Xảy ra lỗi trong quá trình xóa !');</script>");
                }
                
            }
        }
        protected void lbtFunction_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "BunkFunction")
            {
                bool flag = true;
                string FunctionName = "delete";
                switch (FunctionName)
                {
                    case "delete":
                        foreach (GridViewRow gvr in grvCoupon.Rows)
                        {
                            if (((CheckBox)gvr.Cells[0].Controls[0]).Checked)
                            {
                                if (!_BCoupon.Delete(int.Parse(grvCoupon.DataKeys[gvr.RowIndex].Value.ToString())))
                                {
                                    flag = false;
                                }
                            }
                        }
                        BindCoupon();
                        break;
                }
                if (flag == false)
                {
                    RegisterClientScriptBlock("NOTE", "<script>alert('Chuyên mục còn bài viết không thể xóa !');</script>");
                }
            }
        }
        protected void grvCoupon_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            AddCheckBoxAll(e);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Control control = e.Row.Cells[3].FindControl("ddlcParent");
                if (control != null)
                {
                    
                }
            }            
        }
        protected void lbtFilter_Click(object sender, EventArgs e)
        {
            
        }

        protected void grvCoupon_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}